package com.mlog.njyjy.ndop.msg.relay.web;

import com.mlog.njyjy.mp.starter.maws.response.Resp;
import com.mlog.njyjy.ndop.msg.common.domain.MessageRecord;
import com.mlog.njyjy.ndop.msg.relay.service.StatsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *
 * </p>
 *
 * @author zyz
 * @version v1.0, 2020/8/7
 */
@Api(value = "消息统计")
@RestController
@CrossOrigin
@RequestMapping("/api/stats")
public class StatsController {

    @Autowired
    private StatsService statsService;

    @ApiOperation(value = "按业务类型统计", tags = "消息统计")
    @GetMapping("/byType")
    public Resp statsByType(@RequestParam String type) {
        List<MessageRecord> messageRecords = statsService.statsByType(type);
        if (messageRecords != null) {
            return Resp.of(messageRecords);
        }
        return Resp.FAIL;
    }

    @ApiOperation(value = "按产品唯一标记码（PID）统计", tags = "消息统计")
    @GetMapping("/byPID")
    public Resp statsByPID(@RequestParam String pid) {
        List<MessageRecord> messageRecords = statsService.statsByPID(pid);
        if (messageRecords != null) {
            return Resp.of(messageRecords);
        }
        return Resp.FAIL;
    }

    @ApiOperation(value = "按错误码统计", tags = "消息统计")
    @GetMapping("/byErrCode")
    public Resp statsByErrCode(@RequestParam String errodCode) {
        List<MessageRecord> messageRecords = statsService.statsByErrCode(errodCode);
        if (messageRecords != null) {
            return Resp.of(messageRecords);
        }
        return Resp.FAIL;
    }


    @ApiOperation(value = "按错误码和业务类型统计", tags = "消息统计")
    @GetMapping("/byErrCodeAndType")
    public Resp statsByErrCodeAndType(@RequestParam String errodCode, @RequestParam String type) {
        List<MessageRecord> messageRecords = statsService.statsByErrCodeAndType(errodCode, type);
        if (messageRecords != null) {
            return Resp.of(messageRecords);
        }
        return Resp.FAIL;
    }

    @ApiOperation(value = "按错误码和PID统计", tags = "消息统计")
    @GetMapping("/byErrCodeAndPID")
    public Resp statsByErrCodeAndPID(@RequestParam String errodCode, @RequestParam String pid) {
        List<MessageRecord> messageRecords = statsService.statsByErrCodeAndPID(errodCode, pid);
        if (messageRecords != null) {
            return Resp.of(messageRecords);
        }
        return Resp.FAIL;
    }
}
